package com.trilink.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Component;

import com.trilink.common.client.HttpConnect;

@Component
@Lazy(true)
public class MyRegistryBean implements ApplicationContextAware,BeanDefinitionRegistryPostProcessor{

    private ApplicationContext ctx;
    //最后执行
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    	 //Map<String, Object> map=beanFactory.getBeansWithAnnotation(AnalysisActuator.class);
    	 //for(String key:map.keySet()) {
    	//	 System.out.println("key:"+key);
    	// }
    }
  //第二执行
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
    	// 需要被代理的接口,可以通过扫描包来查找对应的类
    	List<Class>classList=new ArrayList<Class>();
    	ClassPathScanningCandidateComponentProvider p=new ClassPathScanningCandidateComponentProvider(false);
    	p.addIncludeFilter(new TypeFilter() {
			public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
					throws IOException {
				AnnotationMetadata an=metadataReader.getAnnotationMetadata();
				Set<String> string=an.getAnnotationTypes();
				String annName=HttpConnect.class.getName();
				ClassMetadata classm=metadataReader.getClassMetadata();
				if(string.contains(annName)) {
					try {
						classList.add(Class.forName(classm.getClassName()));
					} catch (ClassNotFoundException e) {
					}
				}
				return false;
			}
		});
    	p.findCandidateComponents("com.trilink.common");
    	for(Class<?>cls:classList) {
    		
    		//Class<?> cls = Hello.class;
    		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(cls);
    		GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
    		definition.getPropertyValues().add("interfaceClass", definition.getBeanClassName());
    		definition.setBeanClass(MyProxyFactory.class);
    		definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
    		// 注册bean名,一般为类名首字母小写
    		String name=cls.getName();
    		name=getClassName(name);
    		beanDefinitionRegistry.registerBeanDefinition(name, definition);
    	}
    }
    //最先执行
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
    	this.ctx = ctx;
    }
    /**
     * 将类转换成首字母小写的名字
     * @param name
     * @return
     */
    public String getClassName(String name) {
    	int index=name.lastIndexOf(".");
		if(index>0) {
			name=name.substring(index+1,name.length());
		}
		char[] nameChar=name.toCharArray();
		nameChar[0]=(char) (nameChar[0]+32);
		name=new String(nameChar);
		return name;
    }
}